package org.erepublik.company.service;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.Date;
import java.util.HashMap;

import org.erepublik.company.Battle;
import org.erepublik.company.Configuration;
import org.erepublik.company.Hospital;

public class HospitalService {

    private JSONBattleService battleService = new JSONBattleService();
    private JSONHospitalService hospitalSercice = new JSONHospitalService();
    
    public Hospital[] getActiveHospitals() {
        
        battleService.update();
        hospitalSercice.update();
        
        ArrayList<Hospital> hospitals = new ArrayList<Hospital>();
        HashMap<String, Battle> battles = new HashMap<String, Battle>();
        for (Battle battle : battleService.allProducts) {
            if ( battles.get( battle.getRegion()) !=null && battles.get(battle.getRegion()).getBattleStartTime() > battle.getBattleStartTime()  )
                continue;
            
            battles.put(battle.getRegion(), battle);
            
            String regionName = battle.getRegion();
            ArrayList<Hospital> hospitalsInThusRegion = hospitalSercice.getHospitalsInRegion(regionName);
            setHospital(battle, hospitalsInThusRegion);
            hospitals.addAll(hospitalsInThusRegion);
        }
        
        return removePastHospitals(hospitals);
    }

    
    private Hospital[] removePastHospitals(ArrayList<Hospital> hospitals) {
        long now = new Date().getTime() - 1000l*60*60*Configuration.serverTimeDifference;
        
        ArrayList<Hospital> filteredHospitals = new ArrayList<Hospital>();
        for (int i=0; i < hospitals.size(); i++) {
            System.out.println( hospitals.get(i).getBattle().getAttacker() + " " +   new Date(now) + " " + new Date(hospitals.get(i).getUsageTime()) + new Date(hospitals.get(i).getBattle().getBattleStartTime()) + " " + hospitals.get(i).getRegion() + " " + hospitals.get(i).getRegionName() );
            if (hospitals.get(i).getUsageTime() > now - 1000*60*60*2)
                filteredHospitals.add( hospitals.get(i) );
        }
        System.out.println(filteredHospitals.size());
        Hospital[] hospitalsArray = filteredHospitals.toArray(new Hospital[0]);
        
        Arrays.sort(hospitalsArray);
        
        return hospitalsArray;
    }


    private void setHospital(Battle battle, ArrayList<Hospital> hospitalsInThusRegion) {
        for (Hospital hospital : hospitalsInThusRegion) {
            hospital.setUsageTime( battle.getBattleStartTime() + (hospital.getRegion()-1) * 1000*60*60*2 );
            hospital.setBattle(battle);
        }
        
    }
    
    public static void main(String[] args) {
        HospitalService hospitalService = new HospitalService();
        Hospital[] hospitals = hospitalService.getActiveHospitals();
        System.out.println("hospitals: ");
        for (Hospital hospital : hospitals) {
            System.out.println( new Date(hospital.getUsageTime()) + " " + hospital.getRegionName());
        }
    }
    
    
}
